<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
/**
 * 洗涤小助手-用量计算 控制器
 * @author 咩咩 <liuyangvsms@163.com>
 */
class CalculationController extends AdminController {

    /**
     * 用量计算-总览
     */
    public function index(){
        /*--------------------------测试开始--------------------------*/
//        $name = '名字';
//        error_log('name='.$name."\n", 3, "test.log");//打印日志

//        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
//        $info['p_first'] = 0;
//        $edit_p_first_wash_calculation_product = $db_wash_calculation_function->where("p_first=1")->setField($info);
//        if(!$edit_p_first_wash_calculation_product){
//            $this->error('洗涤必备优先状态还原失败!');
//        }

        /*--------------------------测试结束--------------------------*/

        $this->meta_title = '用量计算-总览';//页面title信息
        $this->assign('title', '用量计算-总览');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品信息
     */
    public function product(){
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品信息表

        $wash_calculation_product = $db_wash_calculation_product->order('id DESC')->field('id,product_name,label,product_url')->select();
        $arr = $wash_calculation_product;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 10);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '产品管理-产品信息';//页面title信息
        $this->assign('title', '产品管理-产品信息');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品信息-新增
     */
    public function product_add(){
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品信息表

        if(IS_POST){
            $product_name = I('post.product_name');
            $use_method = I('post.use_method');
            $label = I('post.label');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Calculation/product/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                $product_url = "/wash_helper/Uploads/Calculation/product/picture/".$upload_info['file']['savename'];
                $info['product_name'] = $product_name;
                $info['use_method'] = $use_method;
                $info['label'] = $label;
                $info['product_url'] = $product_url;
                $add_wash_calculation_product = $db_wash_calculation_product->data($info)->add();
                if($add_wash_calculation_product){
                    $this->redirect('Calculation/product');
                }else{
                    $this->error('保存失败!');
                }
            }
        }

        $this->meta_title = '产品管理-产品信息-新增';//页面title信息
        $this->assign('title', '产品管理-产品信息-新增');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品信息-编辑
     */
    public function product_edit(){
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品信息表

        $id = I('get.id');//产品ID
        $product = $db_wash_calculation_product->where("id='$id'")->find();
        $this->assign('product', $product);

        if(IS_POST){
            $product_name = I('post.product_name');
            $use_method = I('post.use_method');
            $label = I('post.label');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Calculation/product/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $product_url = "/wash_helper/Uploads/Calculation/product/picture/".$upload_info['file']['savename'];
                    $info['product_url'] = $product_url;

                    /*删除文件*/
                    $product_url_path = '..'.$product['product_url'];
                    if(file_exists($product_url_path)){
                        unlink($product_url_path);
                    }
                }
            }

            $info['product_name'] = $product_name;
            $info['use_method'] = $use_method;
            $info['label'] = $label;
            $edit_wash_calculation_product = $db_wash_calculation_product->where("id='$id'")->setField($info);
            if($edit_wash_calculation_product){
                $this->redirect('Calculation/product');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '产品管理-产品信息-编辑';//页面title信息
        $this->assign('title', '产品管理-产品信息-编辑');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品信息-删除
     */
    public function product_del(){
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品信息表

        $id = I('get.id');//产品ID
        $product = $db_wash_calculation_product->where("id='$id'")->find();

        /*删除文件*/
        $product_url_path = '..'.$product['product_url'];
        if(file_exists($product_url_path)){
            unlink($product_url_path);
        }else{
            $this->error('产品图片删除失败，该文件不存在!');
        }

        /*删除图文混排图片*/
        $this->del_images_path($product['use_method'],2);

        $del_wash_calculation_product = $db_wash_calculation_product->where("id='$id'")->delete();
        if($del_wash_calculation_product){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 产品管理-产品信息-详情
     */
    public function product_detail(){
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品信息表

        $id = I('get.id');//产品ID
        $product = $db_wash_calculation_product->where("id='$id'")->find();
        $this->assign('product', $product);

        $this->meta_title = '产品管理-产品信息-详情';//页面title信息
        $this->assign('title', '产品管理-产品信息-详情');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单
     */
    public function func_menu(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表

        $pre1 = $db_wash_calculation_function->where("id=100")->field('id,name,belong_id')->find();
        $this->assign('pre1', $pre1);
        $pre1_id = $pre1['id'];
        $wash = $db_wash_calculation_function->where("belong_id='$pre1_id'")->field('id,name,p_first,img_url')->select();
        $this->assign('wash', $wash);

        $pre2 = $db_wash_calculation_function->where("id=200")->field('id,name')->find();
        $this->assign('pre2', $pre2);
        $pre2_id = $pre2['id'];
        $dirt = $db_wash_calculation_function->where("belong_id='$pre2_id'")->field('id,name,p_first,img_url')->select();
        $this->assign('dirt', $dirt);

        $this->meta_title = '功能管理-功能菜单';//页面title信息
        $this->assign('title', '功能管理-功能菜单');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-一级菜单-编辑
     */
    public function one_func_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表

        $id = I('get.id');//产品ID
        $func = $db_wash_calculation_function->where("id='$id'")->find();
        $this->assign('func', $func);

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $edit_wash_calculation_function = $db_wash_calculation_function->where("id='$id'")->setField($info);
            if($edit_wash_calculation_function){
                $this->redirect('Calculation/func_menu');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '一级菜单-编辑';//页面title信息
        $this->assign('title', '一级菜单-编辑');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-洗涤必备默认
     */
    public function one_func_pre_choice(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $id = I('get.id');
        $pre = $db_wash_calculation_function->where("id='$id'")->field('id,name,p_list')->find();
        $this->assign('pre', $pre);

        $p_list = json_decode($pre['p_list'], true);
        $zz_arr = array();
        foreach($p_list[0] as $pid){
            $zz = $db_wash_calculation_product->where("id='$pid'")->field('product_name,label,product_url')->find();
            if($zz){
                array_push($zz_arr,$zz);
            }
        }
        $this->assign('zz', $zz_arr);

        $pt_arr = array();
        foreach($p_list[1] as $pid){
            $pt = $db_wash_calculation_product->where("id='$pid'")->field('product_name,label,product_url')->find();
            if($pt){
                array_push($pt_arr,$pt);
            }
        }
        $this->assign('pt', $pt_arr);

        $this->meta_title = $pre['name'].'-洗涤必备默认';//页面title信息
        $this->assign('title', $pre['name'].'-洗涤必备默认');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-二级菜单-新增
     */
    public function two_func_add(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表

        $meta_title = I('get.meta_title');
        $id = I('get.id');
        $this->assign('id', $id);

        $belong = $db_wash_calculation_function->where("belong_id=0")->field('id,name')->select();
        $this->assign('belong', $belong);

        if(IS_POST){
            $name = I('post.name');
            $belong_id = I('post.belong_id');
            $p_first = I('post.p_first');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Calculation/function/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                $img_url = "/wash_helper/Uploads/Calculation/function/picture/".$upload_info['file']['savename'];
                $info['name'] = $name;
                $info['belong_id'] = $belong_id;
                if(strlen($p_first) != 0){
                    if($p_first == 1){
                        $p_first_info['p_first'] = 0;
                        $edit_p_first_wash_calculation_product = $db_wash_calculation_function->where("p_first=1")->setField($p_first_info);
                    }
                    $info['p_first'] = $p_first;
                }
                $info['p_list'] = '[[],[]]';
                $info['w_list'] = '[]';
                $info['d_list'] = '[]';
                $info['img_url'] = $img_url;
                $add_wash_calculation_function = $db_wash_calculation_function->data($info)->add();
                $this->redirect('Calculation/func_menu');
            }
        }

        $this->meta_title = $meta_title.'-二级菜单名-新增';//页面title信息
        $this->assign('title', $meta_title.'-二级菜单名-新增');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-二级菜单-编辑
     */
    public function two_func_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表

        $id = I('get.id');
        $func = $db_wash_calculation_function->where("id='$id'")->find();
        $this->assign('func', $func);

        $belong = $db_wash_calculation_function->where("belong_id=0")->field('id,name')->select();
        foreach($belong as $k=>$v){
            if($func['belong_id'] == $v['id']){
                $belong[$k]['selected'] = 1;
            }else{
                $belong[$k]['selected'] = 0;
            }
        }
        $this->assign('belong', $belong);

        if(IS_POST){
            $name = I('post.name');
            $belong_id = I('post.belong_id');
            $p_first = I('post.p_first');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Calculation/function/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $img_url = "/wash_helper/Uploads/Calculation/function/picture/".$upload_info['file']['savename'];
                    $info['img_url'] = $img_url;

                    /*删除文件*/
                    $img_url_path = '..'.$func['img_url'];
                    if(file_exists($img_url_path)){
                        unlink($img_url_path);
                    }
                }
            }

            $info['name'] = $name;
            $info['belong_id'] = $belong_id;
            if(strlen($p_first) != 0){
                if($p_first == 1){
                    $p_first_info['p_first'] = 0;
                    $edit_p_first_wash_calculation_product = $db_wash_calculation_function->where("p_first=1")->setField($p_first_info);
                }
                $info['p_first'] = $p_first;
            }
            $edit_wash_calculation_product = $db_wash_calculation_function->where("id='$id'")->setField($info);
            $this->redirect('Calculation/func_menu');
        }

        $this->meta_title = '二级菜单-编辑';//页面title信息
        $this->assign('title', '二级菜单-编辑');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-二级菜单-删除
     */
    public function two_func_del(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表

        $id = I('get.id');
        $func = $db_wash_calculation_function->where("id='$id'")->find();

        /*删除文件*/
        $img_url_path = '..'.$func['img_url'];
        if(file_exists($img_url_path)){
            unlink($img_url_path);
        }else{
            $this->error('图片删除失败，该文件不存在!');
        }

        $del_wash_calculation_function = $db_wash_calculation_function->where("id='$id'")->delete();
        if($del_wash_calculation_function){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 功能管理-功能菜单-洗涤必备优先
     */
    public function two_func_pre_choice(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $id = I('get.id');
        $pre = $db_wash_calculation_function->where("id='$id'")->field('id,name,p_list')->find();
        $this->assign('pre', $pre);

        $p_list = json_decode($pre['p_list'], true);
        $zz_arr = array();
        foreach($p_list[0] as $pid){
            $zz = $db_wash_calculation_product->where("id='$pid'")->field('product_name,label,product_url')->find();
            if($zz){
                array_push($zz_arr,$zz);
            }
        }
        $this->assign('zz', $zz_arr);

        $pt_arr = array();
        foreach($p_list[1] as $pid){
            $pt = $db_wash_calculation_product->where("id='$pid'")->field('product_name,label,product_url')->find();
            if($pt){
                array_push($pt_arr,$pt);
            }
        }
        $this->assign('pt', $pt_arr);

        $this->meta_title = $pre['name'].'-洗涤必备优先';//页面title信息
        $this->assign('title', $pre['name'].'-洗涤必备优先');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-洗涤搭配选择
     */
    public function two_func_wash_choice(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $id = I('get.id');
        $wash = $db_wash_calculation_function->where("id='$id'")->field('id,name,w_list')->find();
        $this->assign('wash', $wash);

        $w_list = json_decode($wash['w_list'], true);
        $w_arr = array();
        foreach($w_list as $wid){
            $w = $db_wash_calculation_product->where("id='$wid'")->field('product_name,label,product_url')->find();
            if($w){
                array_push($w_arr,$w);
            }
        }
        $this->assign('w', $w_arr);

        $this->meta_title = $wash['name'].'-洗涤搭配选择';//页面title信息
        $this->assign('title', $wash['name'].'-洗涤搭配选择');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-去污必备选择
     */
    public function two_func_dirt_choice(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $id = I('get.id');
        $dirt = $db_wash_calculation_function->where("id='$id'")->field('id,name,d_list')->find();
        $this->assign('dirt', $dirt);

        $d_list = json_decode($dirt['d_list'], true);
        $d_arr = array();
        foreach($d_list as $did){
            $d = $db_wash_calculation_product->where("id='$did'")->field('product_name,label,product_url')->find();
            if($d){
                array_push($d_arr,$d);
            }
        }
        $this->assign('d', $d_arr);

        $this->meta_title = $dirt['name'].'-去污必备选择';//页面title信息
        $this->assign('title', $dirt['name'].'-去污必备选择');//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-三级菜单洗涤必备编辑
     */
    public function three_func_pre_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $meta_title = I('get.meta_title');
        $order = I('get.order');
        $this->assign('order', $order);
        $id = I('get.id');
        $this->assign('id', $id);

        $func = $db_wash_calculation_function->where("id='$id'")->field('p_list')->find();
        $p_list = json_decode($func['p_list'], true);

        $arr = array();
        $product = $db_wash_calculation_product->field('id,product_name,label,product_url')->select();
        foreach($product as $p){
            if(in_array($p['id'],$p_list[$order])){
                $p['checked'] = 1;
            }else{
                $p['checked'] = 0;
            }
            array_push($arr, $p);
        }

        /*按是否选中排序*/
        //先用array_column 多维数组按照纵向（列）取出
        $date = array_column($arr, 'checked');
        //再用array_multisort  结合array_column得到的结果对$arr进行排序
        array_multisort($date,SORT_DESC,$arr);

        $this->assign('arr', $arr);

        if(IS_POST){
            $post_id = I('post.id');
            $post_order = I('post.order');
            $post_product = I('post.product');
            if($post_product){
                $post_func = $db_wash_calculation_function->where("id='$post_id'")->field('p_list')->find();
                $post_p_list = json_decode($post_func['p_list'], true);
                $post_p_list[$post_order] = $post_product;
                $json_post_p_list = json_encode($post_p_list, true);

                //更新洗涤必备数据库
                $info['p_list'] = $json_post_p_list;
                $db_wash_calculation_function->where("id='$post_id'")->setField($info);
            }
        }

        $this->meta_title = $meta_title;//页面title信息
        $this->assign('title', $meta_title);//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-三级菜单洗涤必备优先编辑
     */
    public function three_func_pre_first_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $meta_title = I('get.meta_title');
        $order = I('get.order');
        $this->assign('order', $order);
        $id = I('get.id');
        $this->assign('id', $id);

        $func = $db_wash_calculation_function->where("id='$id'")->field('p_list')->find();
        $p_list = json_decode($func['p_list'], true);

        $arr = array();
        $product = $db_wash_calculation_product->field('id,product_name,label,product_url')->select();
        foreach($product as $p){
            if(in_array($p['id'],$p_list[$order])){
                $p['checked'] = 1;
            }else{
                $p['checked'] = 0;
            }
            array_push($arr, $p);
        }

        /*按是否选中排序*/
        //先用array_column 多维数组按照纵向（列）取出
        $date = array_column($arr, 'checked');
        //再用array_multisort  结合array_column得到的结果对$arr进行排序
        array_multisort($date,SORT_DESC,$arr);

        $this->assign('arr', $arr);

        if(IS_POST){
            $post_id = I('post.id');
            $post_order = I('post.order');
            $post_product = I('post.product');
            if(!$post_product){
                $post_product = array();
            }

            $post_func = $db_wash_calculation_function->where("id='$post_id'")->field('p_list')->find();
            $post_p_list = json_decode($post_func['p_list'], true);
            $post_p_list[$post_order] = $post_product;
            $json_post_p_list = json_encode($post_p_list, true);

            //更新洗涤必备数据库
            $info['p_list'] = $json_post_p_list;
            $db_wash_calculation_function->where("id='$post_id'")->setField($info);
        }

        $this->meta_title = $meta_title;//页面title信息
        $this->assign('title', $meta_title);//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-三级菜单洗涤搭配编辑
     */
    public function three_func_wash_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $meta_title = I('get.meta_title');
        $id = I('get.id');
        $this->assign('id', $id);

        $func = $db_wash_calculation_function->where("id='$id'")->field('w_list')->find();
        $w_list = json_decode($func['w_list'], true);

        $arr = array();
        $product = $db_wash_calculation_product->field('id,product_name,label,product_url')->select();
        foreach($product as $p){
            if(in_array($p['id'],$w_list)){
                $p['checked'] = 1;
            }else{
                $p['checked'] = 0;
            }
            array_push($arr, $p);
        }

        /*按是否选中排序*/
        //先用array_column 多维数组按照纵向（列）取出
        $date = array_column($arr, 'checked');
        //再用array_multisort  结合array_column得到的结果对$arr进行排序
        array_multisort($date,SORT_DESC,$arr);

        $this->assign('arr', $arr);

        if(IS_POST){
            $post_id = I('post.id');
            $post_product = I('post.product');
            if(!$post_product){
                $post_product = array();
            }

            $post_w_list = $post_product;
            $json_post_w_list = json_encode($post_w_list, true);

            //更新洗涤搭配数据库
            $info['w_list'] = $json_post_w_list;
            $db_wash_calculation_function->where("id='$post_id'")->setField($info);
        }

        $this->meta_title = $meta_title;//页面title信息
        $this->assign('title', $meta_title);//页面标题
        $this->display();
    }

    /**
     * 功能管理-功能菜单-三级菜单去污必备编辑
     */
    public function three_func_dirt_edit(){
        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
        $db_wash_calculation_product = M('wash_calculation_product');//实例化产品表

        $meta_title = I('get.meta_title');
        $id = I('get.id');
        $this->assign('id', $id);

        $func = $db_wash_calculation_function->where("id='$id'")->field('d_list')->find();
        $d_list = json_decode($func['d_list'], true);

        $arr = array();
        $product = $db_wash_calculation_product->field('id,product_name,label,product_url')->select();
        foreach($product as $p){
            if(in_array($p['id'],$d_list)){
                $p['checked'] = 1;
            }else{
                $p['checked'] = 0;
            }
            array_push($arr, $p);
        }

        /*按是否选中排序*/
        //先用array_column 多维数组按照纵向（列）取出
        $date = array_column($arr, 'checked');
        //再用array_multisort  结合array_column得到的结果对$arr进行排序
        array_multisort($date,SORT_DESC,$arr);

        $this->assign('arr', $arr);

        if(IS_POST){
            $post_id = I('post.id');
            $post_product = I('post.product');
            if(!$post_product){
                $post_product = array();
            }

            $post_d_list = $post_product;
            $json_post_d_list = json_encode($post_d_list, true);

            //更新洗涤搭配数据库
            $info['d_list'] = $json_post_d_list;
            $db_wash_calculation_function->where("id='$post_id'")->setField($info);
        }

        $this->meta_title = $meta_title;//页面title信息
        $this->assign('title', $meta_title);//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-一级菜单-展示
     */
    public function clothes_menu(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $clothes = $db_wash_calculation_clothes->where("belong_id=1")->field('id,name')->select();
        $this->assign('list', $clothes);

        $this->meta_title = '衣物菜单-一级菜单';//页面title信息
        $this->assign('title', '衣物菜单-一级菜单');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-一级菜单-新增
     */
    public function one_clothes_add(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $info['belong_id'] = 1;
            $info['dosage_list'] = '{}';
            $add_wash_calculation_clothes = $db_wash_calculation_clothes->data($info)->add();
            if($add_wash_calculation_clothes){
                $this->redirect('Calculation/clothes_menu');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '衣物菜单-一级菜单-新增';//页面title信息
        $this->assign('title', '衣物菜单-一级菜单-新增');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-一级菜单-编辑
     */
    public function one_clothes_edit(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $clothes = $db_wash_calculation_clothes->where("id='$id'")->find();
        $this->assign('clothes', $clothes);

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $edit_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->setField($info);
            if($edit_wash_calculation_clothes){
                $this->redirect('Calculation/clothes_menu');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '衣物菜单-一级菜单-编辑';//页面title信息
        $this->assign('title', '衣物菜单-一级菜单-编辑');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-一级菜单-删除
     */
    public function one_clothes_del(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $wash_calculation_clothes = $db_wash_calculation_clothes->where("belong_id='$id'")->find();
        if($wash_calculation_clothes){
            $this->error('该菜单下存在栏目，请逐级删除!');
        }else{
            $del_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->delete();
            if($del_wash_calculation_clothes){
                $this->success('删除成功!');
            }else{
                $this->error('删除失败!');
            }
        }
    }

    /**
     * 衣物管理-衣物菜单-二级菜单-展示
     */
    public function two_clothes_menu(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $this->assign('id', $id);
        $up_clothes = $db_wash_calculation_clothes->where("id='$id'")->field('id,name')->find();
        $this->assign('up_clothes', $up_clothes);

        $clothes = $db_wash_calculation_clothes->where("belong_id='$id'")->field('id,name')->select();
        $this->assign('list', $clothes);

        $this->meta_title = '衣物菜单-'.$up_clothes['name'].'(二级菜单)';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_clothes['name'].'(二级菜单)');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-二级菜单-新增
     */
    public function two_clothes_add(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $up_clothes = $db_wash_calculation_clothes->where("id='$id'")->field('name')->find();

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $info['belong_id'] = $id;
            $info['dosage_list'] = '{}';
            $add_wash_calculation_clothes = $db_wash_calculation_clothes->data($info)->add();
            if($add_wash_calculation_clothes){
                $this->redirect('Calculation/two_clothes_menu?id='.$id);
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '衣物菜单-'.$up_clothes['name'].'(二级菜单)-新增';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_clothes['name'].'(二级菜单)-新增');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-二级菜单-编辑
     */
    public function two_clothes_edit(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $belong_id = I('get.belong_id');
        $up_clothes = $db_wash_calculation_clothes->where("id='$belong_id'")->field('name')->find();
        $id = I('get.id');
        $clothes = $db_wash_calculation_clothes->where("id='$id'")->field('name')->find();
        $this->assign('clothes', $clothes);

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $edit_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->setField($info);
            if($edit_wash_calculation_clothes){
                $this->redirect('Calculation/two_clothes_menu?id='.$belong_id);
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '衣物菜单-'.$up_clothes['name'].'(二级菜单)-编辑';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_clothes['name'].'(二级菜单)-编辑');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-二级菜单-删除
     */
    public function two_clothes_del(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $wash_calculation_clothes = $db_wash_calculation_clothes->where("belong_id='$id'")->find();
        if($wash_calculation_clothes){
            $this->error('该菜单下存在栏目，请逐级删除!');
        }else{
            $del_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->delete();
            if($del_wash_calculation_clothes){
                $this->success('删除成功!');
            }else{
                $this->error('删除失败!');
            }
        }
    }

    /**
     * 衣物管理-衣物菜单-三级菜单-展示
     */
    public function three_clothes_menu(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        //一级ID
        $up_belong_id = I('get.up_belong_id');
        $this->assign('up_belong_id', $up_belong_id);
        $up_belong = $db_wash_calculation_clothes->where("id='$up_belong_id'")->field('name')->find();

        //二级ID
        $id = I('get.id');
        $this->assign('id', $id);
        $up_clothes = $db_wash_calculation_clothes->where("id='$id'")->field('name')->find();

        $clothes = $db_wash_calculation_clothes->where("belong_id='$id'")->field('id,name,weight,img_url')->select();
        $arr = $clothes;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 15);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-三级菜单-新增
     */
    public function three_clothes_add(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $up_belong_id = I('get.up_belong_id');
        $up_belong = $db_wash_calculation_clothes->where("id='$up_belong_id'")->field('name')->find();
        $id = I('get.id');
        $up_clothes = $db_wash_calculation_clothes->where("id='$id'")->field('name')->find();

        if(IS_POST){
            $name = I('post.name');
            $weight = I('post.weight');

            /*文件上传*/
            $savename = time().'_'.mt_rand(1000000,9999999);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
            $upload->savePath = './Calculation/clothes/picture/'; // 设置附件上传（子）目录
            $upload->saveName = $savename; // 上传文件的文件名
            //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
            $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
            $upload->replace = true; // true会存在同名文件将覆盖
            // 上传文件
            $upload_info = $upload->upload();
            if (!$upload_info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else {
                $img_url = "/wash_helper/Uploads/Calculation/clothes/picture/" . $upload_info['file']['savename'];

                $info['name'] = $name;
                $info['weight'] = $weight;
                $info['belong_id'] = $id;
                $info['dosage_list'] = '{}';
                $info['img_url'] = $img_url;
                $add_wash_calculation_clothes = $db_wash_calculation_clothes->data($info)->add();
                if ($add_wash_calculation_clothes) {
                    $this->redirect('Calculation/three_clothes_menu?up_belong_id='.$up_belong_id.'&id='.$id);
                } else {
                    $this->error('保存失败!');
                }
            }
        }

        $this->meta_title = '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)-新增';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)-新增');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-三级菜单-编辑
     */
    public function three_clothes_edit(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $up_belong_id = I('get.up_belong_id');
        $up_belong = $db_wash_calculation_clothes->where("id='$up_belong_id'")->field('name')->find();
        $belong_id = I('get.belong_id');
        $up_clothes = $db_wash_calculation_clothes->where("id='$belong_id'")->field('name')->find();
        $id = I('get.id');
        $clothes = $db_wash_calculation_clothes->where("id='$id'")->field('name,weight,img_url')->find();
        $this->assign('clothes', $clothes);

        if(IS_POST){
            $name = I('post.name');
            $weight = I('post.weight');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Calculation/clothes/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $img_url = "/wash_helper/Uploads/Calculation/clothes/picture/".$upload_info['file']['savename'];
                    $info['img_url'] = $img_url;

                    /*删除文件*/
                    $clothes_url_path = '..'.$clothes['img_url'];
                    if(file_exists($clothes_url_path)){
                        unlink($clothes_url_path);
                    }
                }
            }

            $info['name'] = $name;
            $info['weight'] = $weight;
            $edit_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->setField($info);
            if($edit_wash_calculation_clothes){
                $this->redirect('Calculation/three_clothes_menu?up_belong_id='.$up_belong_id.'&id='.$belong_id);
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)-编辑';//页面title信息
        $this->assign('title', '衣物菜单-'.$up_belong['name'].'(二级菜单)-'.$up_clothes['name'].'(三级菜单)-编辑');//页面标题
        $this->display();
    }

    /**
     * 衣物管理-衣物菜单-三级菜单-删除
     */
    public function three_clothes_del(){
        $db_wash_calculation_clothes = M('wash_calculation_clothes');//实例化衣物表

        $id = I('get.id');
        $clothes = $db_wash_calculation_clothes->where("id='$id'")->find();

        /*删除文件*/
        $clothes_url_path = '..'.$clothes['img_url'];
        if(file_exists($clothes_url_path)){
            unlink($clothes_url_path);
        }else{
            $this->error('衣物图片删除失败，该文件不存在!');
        }

        $del_wash_calculation_clothes = $db_wash_calculation_clothes->where("id='$id'")->delete();
        if($del_wash_calculation_clothes){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 删除图文混排图片
     * str  $str    要提去的字符串
     * int  $type   1提取图片 2提取src和href
     */
    public function del_images_path($str,$type=1){
        if($type == 1){
            preg_match('/<img.+src=\"?(.+\.(jpg|gif|jpeg|png))\"?.+>/i',$str,$match);
            if($match[1]){
                foreach($match[1] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
        if($type == 2){
            preg_match_all('/(src|href)=\"?(.+\.(jpg|gif|jpeg|png))\"/i',$str,$match);
            if($match[2]){
                foreach($match[2] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
    }
}
